package cn.yuemouren.leetcode;

/**
 * @Author: Timi
 * @Description: 两数相加
 * @Date: 2020/8/18 16:41
 */
public class Test_2 {

    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0);
        ListNode n1 = l1,n2 = l2,dummyHead = head;
        int camry = 0;

        while (null != n1 || null != n2){
            int x = null != n1 ? n1.val : 0;
            int y = null != n2 ? n2.val : 0;
            int sum = x + y + camry;
            camry = sum / 10;
            ListNode node = new ListNode(sum % 10);
            dummyHead = dummyHead.next = node;

            if(null != n1)
                n1 = n1.next;

            if(null != n2)
                n2 = n2.next;
        }
        if(camry > 0){
            dummyHead.next = new ListNode(camry);
        }
        return head.next;
    }
}
